package com.ef.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.ECallStatus;
import com.ef.constant.ELeadsDealType;
import com.ef.constant.ELeadsEventType;
import com.ef.constant.EUserTodoObject;
import com.ef.constant.EUserTodoStatus;
import com.ef.constant.EUserTodoType;
import com.ef.domain.ClassLevel;
import com.ef.domain.ClassType;
import com.ef.domain.ClassUnit;
import com.ef.domain.Contract;
import com.ef.domain.Course;
import com.ef.domain.Leads;
import com.ef.domain.LeadsClassEntity;
import com.ef.domain.LeadsDealHistory;
import com.ef.domain.LeadsEventHistory;
import com.ef.domain.User;
import com.ef.domain.UserTodoEvent;
import com.ef.service.ClassLevelService;
import com.ef.service.ClassTypeService;
import com.ef.service.ContractService;
import com.ef.service.CourseService;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsDealHistoryService;
import com.ef.service.LeadsEventHistoryService;
import com.ef.service.TodoEventService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;
import com.ef.util.LeadsExample;

public class PersonalTouchAction extends LeadsAction {

	private static final long serialVersionUID = 1L;

	private ClassLevelService classLevelService;

	private ClassTypeService classTypeService;

	private CourseService courseService;
	/**
	 * 鍙鐨凜lassType
	 */
	private List<ClassType> optionalClassTypeList;

	private List<ClassUnit> selectClassUnitList;

	private Map<String, String> courseList;

	private Map<String, String> classLevelList;

	private String selClassLevelId;

	private String selCourseId;

	private String selLeadsId;

	//private LeadsEventHistoryService leadsEventHistoryService;

	private TodoEventService todoEventService;

	//private List<LeadsEventHistory> calls;

	private Long currentCallId;

	private String currentCallTelephone;

	private Date currentCallDealTime;

	private ECallStatus currentCallStatus = ECallStatus.Connected;

	private String currentCallComments;

	private Long todoEventId;

	private EUserTodoType todoEventType;

	private String todoEventComments;

	private Date todoEventDate;

	private String todoEventTime;

	private List<LeadsClassEntity> leadsClassEntityList;

	private LeadsClassEntityService leadsClassEntityService;

	private List<LeadsDealHistory> upperLevelHistory;

	private List<LeadsDealHistory> deferHistory;

	private List<LeadsDealHistory> upperClassHistory;

	private LeadsDealHistoryService leadsDealHistoryService;

	private List<Contract> leadsContracts;

	private ContractService contractService;

	public List<Contract> getLeadsContracts() {
		return leadsContracts;
	}

	public void setLeadsContracts(List<Contract> leadsContracts) {
		this.leadsContracts = leadsContracts;
	}

	public ContractService getContractService() {
		return contractService;
	}

	public void setContractService(ContractService contractService) {
		this.contractService = contractService;
	}

	private Integer year;

	private Integer week;

	private Boolean newDefer = false;

	private Boolean delDefer = false;

	public Boolean getDelDefer() {
		return delDefer;
	}

	public void setDelDefer(Boolean delDefer) {
		this.delDefer = delDefer;
	}

	public PersonalTouchAction() {
		// 获取当前是某年某周
		// @NOTE 需要对year进行处理
		Calendar now = Calendar.getInstance();
		now.setFirstDayOfWeek(Calendar.MONDAY);
		int day = now.get(Calendar.DAY_OF_YEAR);
		week = now.get(Calendar.WEEK_OF_YEAR);
		year = now.get(Calendar.YEAR);
		if (day > 7 && week == 1) {
			year++;
		}
	}

	@SuppressWarnings("serial")
	public String list() {
		fetchListData();
		CustomExample<Leads> example = new LeadsExample(this.getModel(),
				Leads.class) {
			public void appendCondition(Criteria criteria) {
				super.appendCondition(criteria);
				criteria.add(Restrictions.isNotNull("eliteCard"));
				User user = getSessionUser();
				if (user.getRole() != null
						&& user.getRole().getName().equalsIgnoreCase("CR")) {
					// CR只能看自己的
					criteria.createCriteria("cr").add(
							Restrictions.eq("id", getSessionUser().getId()));
				}
				criteria.addOrder(Order.desc("createTime"));
			}
		};
		example.addBetweenDateProperty("createTime", selCreateStartTime,
				selCreateEndTime);
		example.addBetweenDateProperty("setApptTime", selSetApptStartTime,
				selSetApptEndTime);
		example.addBetweenDateProperty("showTime", selShowStartTime,
				selShowEndTime);
		example.addBetweenDateProperty("lastEventTime", selLastTouchStartTime,
				selLastTouchEndTime);
		example.addBetweenDateProperty("payTime", selPayStartTime,
				selPayEndTime);
		example.addBetweenDateProperty("tmAssignTime", selAssignTmStartTime,
				selAssignTmEndTime);
		example.addBetweenDateProperty("originalClassTime",
				selOriginalClassTimeStartTime, selOriginalClassTimeEndTime);
		this.listResult = leadsService.findByExample(example, this
				.getStartIndex(), this.getPageSize());
		return LIST;
	};

	public String saveStudent() {
		Leads leads = this.getModel();
		String classUnitString = getRequest().getParameter("classUnitString");
		ArrayList<ClassUnit> classUnits = null;
		if (classUnitString != null && classUnitString.trim().length() > 0) {
			classUnits = new ArrayList<ClassUnit>();
			String[] cidLimit = classUnitString.split(";");
			for (String cl : cidLimit) {
				String[] c = cl.split(",");
				ClassUnit unit = new ClassUnit();
				unit.setClassType(classTypeService.findById(Long.valueOf(c[0]
						.trim())));
				unit.setUpdateLimitTimes(Integer.valueOf(c[1].trim()));
				classUnits.add(unit);
			}
		}
		leads.setClassUnits(classUnits);
		if (selClassLevelId != null && selClassLevelId.trim().length() > 0) {
			leads.setCurrentlevel(classLevelService.findById(Long
					.valueOf(selClassLevelId.trim())));
		}
		if (selCourseId != null && selCourseId.trim().length() > 0) {
			leads.setCourse(courseService.findById(Long.valueOf(selCourseId
					.trim())));
		}
		leadsService.saveOrUpdate(leads);
		this.resultOK("save Student prop successful", "list");
		fetchListData();
		return EDIT;
	}

	public String bookingEdit() {
		fetchListData();
		int trueWeek = week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, trueWeek, this
						.getModel());
		return EDIT;
	}

	public String bookingSave() {
		saveStudent();
		fetchListData();
		int trueWeek = week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, trueWeek, this
						.getModel());
		return EDIT;
	}

	public String visitEdit() {
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(entity);
		return "visitEdit";
	}

	private Date selTrueValidEndTime;
	private Date selTrueValidStartTime;
	private Date selDeferStartTime;
	private Date selDeferEndTime;

	// 保存延期信息
	public String deferSave() {
		Leads leads = this.getModel();
		if (leads.getValidStartTime() == null
				|| leads.getValidEndTime() == null) {
			this.resultError("no contract times ", "list");
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}
		if (selTrueValidStartTime == null || selTrueValidEndTime == null) {
			this.resultError("full the true End Time and Defer times ", "list");
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}
		// 原有延期开始时间
		Date oldDeferStartTime = leads.getDeferStartTime();
		// 原有延期结束时间
		Date oldDeferEndTime = leads.getDeferEndTime();
		// 原有正式开始时间
		Date oldTrueStart = leads.getTrueValidStartTime();
		// 原有正式结束时间
		Date oldTrueEnd = leads.getTrueValidEndTime();
		// 设置的正式开始
		Date trueStart = CalendarUtil.getBeginOfDay(selTrueValidStartTime);
		// 设置的正式结束
		Date trueEnd = CalendarUtil.getBeginOfDay(selTrueValidEndTime);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String oldSStr = "";
		String oldEStr = "";
		String oldTEStr = "";
		String oldTSStr = "";
		String newSStr = "";
		String newEStr = "";
		String newTEStr = "";
		String newTSStr = "";
		if (oldDeferStartTime != null) {
			oldSStr = df.format(oldDeferStartTime);
		}
		if (oldDeferEndTime != null) {
			oldEStr = df.format(oldDeferEndTime);
		}
		if (oldTrueEnd != null) {
			oldTEStr = df.format(oldTrueEnd);
		}
		if (oldTrueStart != null) {
			oldTSStr = df.format(oldTrueStart);
		}
		if (oldTrueEnd == null) {
			oldTrueEnd = leads.getValidEndTime();
		}
		if (oldTrueStart == null) {
			oldTrueStart = leads.getValidStartTime();
		}
		if (selDeferStartTime != null) {
			newSStr = df.format(selDeferStartTime);
		}
		if (selDeferEndTime != null) {
			newEStr = df.format(selDeferEndTime);
		}
		if (selTrueValidEndTime != null) {
			newTEStr = df.format(selTrueValidEndTime);
		}
		if (selTrueValidStartTime != null) {
			newTSStr = df.format(selTrueValidStartTime);
		}
		// 没有设置延期时间，修改原有时间，只能向前调整
		if (selDeferStartTime == null && selDeferEndTime == null && !delDefer) {
			if (selTrueValidStartTime.equals(oldTrueStart)
					&& selTrueValidEndTime.equals(oldTrueEnd)) {
				resultError("please change the data!", "list");
			} else if (trueStart.after(trueEnd)) {
				resultError("start time mast before end time", "list");
			} else {
				if (isManager()) {
					LeadsDealHistory dHis = new LeadsDealHistory();
					dHis.setLeads(leads);
					dHis.setOperUser(getSessionUser());
					dHis.setDealTime(new Date());
					dHis.setType(ELeadsDealType.Defer);
					dHis.setComments("Start from " + oldTSStr + " to "
							+ newTSStr + ";End from " + oldTEStr + " to "
							+ newTEStr);
					leadsDealHistoryService.saveOrUpdate(dHis);
					leads.setTrueValidStartTime(selTrueValidStartTime);
					leads.setTrueValidEndTime(selTrueValidEndTime);
					saveStudent();
				} else {
					resultError("only Manager Can do this work", "list");
				}
			}
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}
		// 删除defer，时间全部缩回
		if (delDefer) {
			if (oldDeferEndTime == null || oldDeferStartTime == null) {
				this.resultError("no old defer time", "list");
			} else {
				long tmpnewTrueEnd = oldTrueEnd.getTime()
						- (oldDeferEndTime.getTime() - oldDeferStartTime
								.getTime());
				Date newTrueEnd = new Date(tmpnewTrueEnd);
				LeadsDealHistory dHis = new LeadsDealHistory();
				dHis.setLeads(leads);
				dHis.setOperUser(getSessionUser());
				dHis.setDealTime(new Date());
				dHis.setType(ELeadsDealType.Defer);
				dHis.setComments("studend delete defer end time  from "
						+ oldTEStr + " to " + df.format(newTrueEnd));
				leadsDealHistoryService.saveOrUpdate(dHis);
				leads.setTrueValidEndTime(newTrueEnd);
				leads.setDeferStartTime(null);
				leads.setDeferEndTime(null);
				leads.setDeferType(null);
				saveStudent();
			}
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}// end delete Defer

		if (selDeferStartTime == null || selDeferEndTime == null) {
			resultError("please full Defer Start Time,Defer End Time", "list");
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}
		if (selDeferStartTime.after(selDeferEndTime)) {
			resultError("defer start time mast before defer end time", "list");
			fetchListData();
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, week, this
							.getModel());
			return EDIT;
		}
		if (oldDeferStartTime != null && oldDeferEndTime != null) {
			// 已经有defer 再 new
			if (selDeferStartTime.before(oldDeferEndTime)) {
				resultError(
						"new defer start time mast after old defer end time",
						"list");
				fetchListData();
				leadsClassEntityList = leadsClassEntityService
						.getLeadsClassEntityListOrderByBookingTime(year, week,
								this.getModel());
				return EDIT;
			}
		}
		if (selDeferStartTime.after(oldTrueEnd)
				|| selDeferStartTime.before(oldTrueStart)) {
			resultError("defer start time mast in valid time", "list");
		} else {
			long tmpnewTrueEnd = selDeferEndTime.getTime()
					- selDeferStartTime.getTime() + oldTrueEnd.getTime();
			Date newTrueEnd = new Date(tmpnewTrueEnd);
			LeadsDealHistory dHis = new LeadsDealHistory();
			dHis.setLeads(leads);
			dHis.setOperUser(getSessionUser());
			dHis.setDealTime(new Date());
			dHis.setType(ELeadsDealType.Defer);
			String type = "";
			if (leads.getDeferType() != null) {
				type = leads.getDeferType().name();
			}
			dHis.setComments("studend defer " + type + " from " + newSStr
					+ " to " + newEStr + " so true end time  from " + oldTEStr
					+ " to " + df.format(newTrueEnd));
			leadsDealHistoryService.saveOrUpdate(dHis);
			leads.setTrueValidEndTime(newTrueEnd);
			leads.setDeferStartTime(selDeferStartTime);
			leads.setDeferEndTime(selDeferEndTime);
			saveStudent();
		}
		fetchListData();
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, week, this
						.getModel());
		return EDIT;
	}

	// 升级
	public String updateLevelSave() {
		Leads leads = this.getModel();
		String oldLevel = "no Level";
		String oldLevelId = "-1";
		if (leads.getCurrentlevel() != null) {
			oldLevelId = leads.getCurrentlevel().getId().toString();
			oldLevel = leads.getCurrentlevel().getName();
		}
		if (selClassLevelId != null && !selClassLevelId.equals(oldLevelId)) {
			LeadsDealHistory dHis = new LeadsDealHistory();
			dHis.setLeads(leads);
			dHis.setOperUser(getSessionUser());
			dHis.setDealTime(new Date());
			String newLevel = classLevelService.findById(
					Long.valueOf(selClassLevelId.trim())).getName();
			dHis.setPreState(oldLevel);
			dHis.setCurState(newLevel);
			dHis.setType(ELeadsDealType.UpdateLevel);
			dHis.setComments("studend update level from " + oldLevel + " to "
					+ newLevel);
			leadsDealHistoryService.saveOrUpdate(dHis);
		}
		saveStudent();
		fetchListData();
		int trueWeek = week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, trueWeek, this
						.getModel());
		return EDIT;
	}

	public Boolean isManager() {
		if (getSessionUser().getRole().getName().equalsIgnoreCase("CR")) {
			return false;
		} else {
			return true;
		}
	}

	// 升课
	public String updateClassSave() {
		String classUnitString = getRequest().getParameter("classUnitString");
		Leads leads = this.getModel();
		String oldClass = "no Class";
		String newClass = "";
		StringBuffer classStr = new StringBuffer();
		if (leads.getClassUnits() != null && leads.getClassUnits().size() > 0) {
			List<ClassUnit> allUnit = leads.getClassUnits();
			for (ClassUnit unit : allUnit) {
				classStr.append(unit.getClassType().getName()).append("[")
						.append(unit.getUpdateLimitTimes()).append("]");
			}
			oldClass = classStr.toString();
		}
		classStr = new StringBuffer();
		if (classUnitString != null && classUnitString.trim().length() > 0) {
			String[] cidLimit = classUnitString.split(";");
			for (String cl : cidLimit) {
				String[] c = cl.split(",");
				ClassType tType = classTypeService.findById(Long.valueOf(c[0]
						.trim()));
				Integer tL = Integer.valueOf(c[1].trim());
				classStr.append(tType.getName()).append("[").append(
						tL.toString()).append("]");

			}
			newClass = classStr.toString();
		}
		if (newClass.trim().length() > 0
				&& !newClass.equalsIgnoreCase(oldClass)) {
			LeadsDealHistory dHis = new LeadsDealHistory();
			dHis.setLeads(leads);
			dHis.setOperUser(getSessionUser());
			dHis.setDealTime(new Date());
			dHis.setPreState(oldClass);
			dHis.setCurState(newClass);
			dHis.setType(ELeadsDealType.UpdateClass);
			dHis.setComments("studend update Class from " + oldClass + " to "
					+ newClass);
			leadsDealHistoryService.saveOrUpdate(dHis);
		}
		saveStudent();
		fetchListData();
		int trueWeek = week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, trueWeek, this
						.getModel());
		return EDIT;
	}

	public String visitSave() {
		save();
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(entity);
		return "visitEdit";
	}

	protected void fetchListData() {
		super.fetchListData();
		Leads tmpLeads = getModel();
		Map<String, String> map = new LinkedHashMap<String, String>();
		List<ClassLevel> classLevel = classLevelService.findALL();
		for (ClassLevel cl : classLevel) {
			map.put(cl.getId().toString(), cl.getName());
		}
		classLevelList = map;

		Map<String, String> map2 = new LinkedHashMap<String, String>();
		List<Course> courses = courseService.findALL();
		for (Course c : courses) {
			map2.put(c.getId().toString(), c.getName());
		}
		courseList = map2;

		List<ClassType> allClassTypelist = classTypeService.findALLValid();
		if (tmpLeads.getId() != null && tmpLeads.getClassUnits() != null) {
			selectClassUnitList = tmpLeads.getClassUnits();
			for (ClassUnit cu : selectClassUnitList) {
				allClassTypelist.remove(cu.getClassType());
			}
			optionalClassTypeList = allClassTypelist;
		} else {
			optionalClassTypeList = allClassTypelist;
			selectClassUnitList = new ArrayList<ClassUnit>();
		}
		if (tmpLeads.getId() != null && tmpLeads.getCourse() != null) {
			this.selCourseId = tmpLeads.getCourse().getId().toString();
		}
		if (tmpLeads.getId() != null && tmpLeads.getCurrentlevel() != null) {
			this.selClassLevelId = tmpLeads.getCurrentlevel().getId()
					.toString();
		}
		if (tmpLeads.getId() != null) {
			this.upperClassHistory = this.leadsDealHistoryService.findByLeads(
					tmpLeads, ELeadsDealType.UpdateClass);
			this.upperLevelHistory = this.leadsDealHistoryService.findByLeads(
					tmpLeads, ELeadsDealType.UpdateLevel);
			this.deferHistory = this.leadsDealHistoryService.findByLeads(
					tmpLeads, ELeadsDealType.Defer);
		}
		if (tmpLeads.getId() != null) {
			leadsContracts = contractService
					.findValidContract(tmpLeads.getId());
		}
	}

	/**
	 * 生效某个合同
	 * 
	 * @return
	 */
	public String effectContract() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String contractId = request.getParameter("contractId");
		Contract contract = contractService.findById(Long.valueOf(contractId
				.trim()));
		if (contract != null && contract.getCourse() == null) {
			fetchListData();
			int trueWeek = week;
			if (year == 2010) {
				trueWeek = trueWeek + 1;
			}
			leadsClassEntityList = leadsClassEntityService
					.getLeadsClassEntityListOrderByBookingTime(year, trueWeek,
							this.getModel());
			this.resultError("no Course", "list");
			return EDIT;
		}
		Leads leads = this.getModel();
		if (contract.getFreeF2FTimes() != null) {
			leads.setFreeF2FCount(contract.getFreeF2FTimes());
		} else {
			leads.setFreeF2FCount(null);
		}
		List<ClassUnit> classUs = new ArrayList<ClassUnit>();
		if (contract.getClassUnits() != null
				&& contract.getClassUnits().size() > 0) {
			for (ClassUnit cu : contract.getClassUnits()) {
				ClassUnit su = new ClassUnit();
				su.setClassType(classTypeService.findById(Long.valueOf(cu
						.getClassType().getId())));
				su.setUpdateLimitTimes(cu.getUpdateLimitTimes());
				classUs.add(su);
			}
			leads.setClassUnits(classUs);
		}
		leads.setCurrentlevel(contract.getBeginLevel());
		leads.setEliteCard(contract.getEliteCard().trim().toLowerCase());
		leads.setIsVIP(contract.getIsVip());
		leads.setStudent(true);
		leads.setNeedJudgeNetLevel(contract.getCourse().getHasNetCourse());
		leads.setValidStartTime(contract.getStartTime());
		leads.setTrueValidStartTime(contract.getStartTime());
		leads.setValidEndTime(contract.getEndTime());
		leads.setTrueValidEndTime(contract.getEndTime());
		leads.setIsStudent(true);
		leads.setDeferStartTime(null);
		leads.setDeferEndTime(null);
		leads.setDeferType(null);
		leads.setCourse(contract.getCourse());
		leadsService.saveOrUpdate(leads);
		LeadsDealHistory dHis = new LeadsDealHistory();
		dHis.setLeads(leads);
		dHis.setOperUser(getSessionUser());
		dHis.setDealTime(new Date());
		dHis.setType(ELeadsDealType.UpdateClass);
		dHis.setComments("effect studend contract " + contract.getContractNo()
				+ " [  " + contractId + "]");
		leadsDealHistoryService.saveOrUpdate(dHis);
		fetchListData();
		int trueWeek = week;
		if (year == 2010) {
			trueWeek = trueWeek + 1;
		}
		leadsClassEntityList = leadsClassEntityService
				.getLeadsClassEntityListOrderByBookingTime(year, trueWeek, this
						.getModel());
		return EDIT;
	}

	private static SimpleDateFormat timeDf = new SimpleDateFormat("K:mm a",
			new Locale("en"));

	public String saveCall() {
		Leads leads = this.saveLeads();
		if (leads == null) {
			this.setResult(ConfirmMessage.MSG_SAVELEADS_ERROR);
			fetchListData();
			calls = leadsEventHistoryService.findByLeads(leads);
			return CALL;
		}
		if (currentCallTelephone != null
				&& currentCallTelephone.trim().length() > 0) {
			LeadsEventHistory currentCall = null;
			if (currentCallId == null) {
				currentCall = new LeadsEventHistory();
				leads.setCrCall(leads.getCrCall() + 1);
			} else {
				currentCall = leadsEventHistoryService.findById(currentCallId);
			}
			currentCall.setComments(currentCallComments);
			currentCall.setCallStatus(currentCallStatus);
			currentCall.setDealTime(new Date());
			currentCall.setEventType(ELeadsEventType.CRCall);
			currentCall.setLeads(leads);
			currentCall.setTelephone(currentCallTelephone);
			currentCall.setOperater(getSessionUser());
			leadsEventHistoryService.saveOrUpdate(currentCall);
			currentCallId = currentCall.getId();
			leads.setLastEventTime(new Date());
			leads.setLastEventCommons(currentCallComments);
			if (todoEventDate != null) { // todoEventDate 不为空处理
				Date time = null;
				if (todoEventTime != null && todoEventTime.trim().length() > 0) {
					try {
						time = timeDf.parse(todoEventTime);
					} catch (ParseException e) {
						System.out.println("TodoEventAction parse time error");
					}
				}
				if (time == null) {
					try {
						time = timeDf.parse("9:00 AM");
					} catch (ParseException e) {
						System.out.println("TodoEventAction parse time error");
					}
				}
				leads.setNextEventTime(CalendarUtil.getDateTime(todoEventDate,
						time));
			}
			leadsService.saveOrUpdate(leads);
		}
		if (todoEventDate != null) { // todoEventDate 涓嶄负绌哄鐞�
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String day = df.format(todoEventDate);
			String title = "NOTE:" + day + " " + todoEventTime + "call "
					+ leads.getName();
			UserTodoEvent todoEvent = null;
			if (todoEventId == null) {
				todoEvent = new UserTodoEvent();
			} else {
				todoEvent = todoEventService.findById(todoEventId);
			}
			todoEvent.setCreateTime(new Date());
			todoEvent.setTodoType(todoEventType);
			todoEvent.setCreateUser(getSessionUser());
			todoEvent.setGoAction("/customer/PersonalTouch/call?id="
					+ leads.getId());
			Date time = null;
			if (todoEventTime != null && todoEventTime.trim().length() > 0) {
				try {
					time = timeDf.parse(todoEventTime);
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			} else {
				try {
					time = timeDf.parse("9:00 AM");
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
			}
			todoEvent.setReminderEndTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			time.setTime(time.getTime() - 30 * 60 * 1000);
			todoEvent.setReminderStartTime(CalendarUtil.getDateTime(
					todoEventDate, time));
			todoEvent.setSelLeads(leads);
			todoEvent.setToObject(EUserTodoObject.MySelf);
			todoEvent.setStatus(EUserTodoStatus.Valid);
			todoEvent.setToUser(getSessionUser());
			todoEvent.setDetail(todoEventComments);
			todoEvent.setTitle(title);
			todoEventService.saveOrUpdate(todoEvent);
			todoEventId = todoEvent.getId();
		}
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(leads);
		leadsClassEntityList = leadsClassEntityService.getLeadsClassEntityList(
				this.getModel(), 20);
		this.setResult(ConfirmMessage.Call);
		return CALL;
	}

	public String call() {
		fetchListData();
		calls = leadsEventHistoryService.findByLeads(this.getModel());
		leadsClassEntityList = leadsClassEntityService.getLeadsClassEntityList(
				this.getModel(), 20);
		return CALL;
	}

//	public List<LeadsEventHistory> getCalls() {
//		return calls;
//	}
//
//	public void setCalls(List<LeadsEventHistory> calls) {
//		this.calls = calls;
//	}

//	public LeadsEventHistoryService getLeadsEventHistoryService() {
//		return leadsEventHistoryService;
//	}
//
//	public void setLeadsEventHistoryService(
//			LeadsEventHistoryService leadsEventHistoryService) {
//		this.leadsEventHistoryService = leadsEventHistoryService;
//	}

	public TodoEventService getTodoEventService() {
		return todoEventService;
	}

	public void setTodoEventService(TodoEventService todoEventService) {
		this.todoEventService = todoEventService;
	}

	public Long getCurrentCallId() {
		return currentCallId;
	}

	public void setCurrentCallId(Long currentCallId) {
		this.currentCallId = currentCallId;
	}

	public String getCurrentCallTelephone() {
		return currentCallTelephone;
	}

	public void setCurrentCallTelephone(String currentCallTelephone) {
		this.currentCallTelephone = currentCallTelephone;
	}

	public ECallStatus getCurrentCallStatus() {
		return currentCallStatus;
	}

	public void setCurrentCallStatus(ECallStatus currentCallStatus) {
		this.currentCallStatus = currentCallStatus;
	}

	public Long getTodoEventId() {
		return todoEventId;
	}

	public void setTodoEventId(Long todoEventId) {
		this.todoEventId = todoEventId;
	}

	public EUserTodoType getTodoEventType() {
		return todoEventType;
	}

	public void setTodoEventType(EUserTodoType todoEventType) {
		this.todoEventType = todoEventType;
	}

	public String getTodoEventComments() {
		return todoEventComments;
	}

	public void setTodoEventComments(String todoEventComments) {
		this.todoEventComments = todoEventComments;
	}

	public Date getTodoEventDate() {
		return todoEventDate;
	}

	public void setTodoEventDate(Date todoEventDate) {
		this.todoEventDate = todoEventDate;
	}

	public String getCurrentCallComments() {
		return currentCallComments;
	}

	public void setCurrentCallComments(String currentCallComments) {
		this.currentCallComments = currentCallComments;
	}

	public Date getCurrentCallDealTime() {
		return currentCallDealTime;
	}

	public void setCurrentCallDealTime(Date currentCallDealTime) {
		this.currentCallDealTime = currentCallDealTime;
	}

	public String getTodoEventTime() {
		return todoEventTime;
	}

	public void setTodoEventTime(String todoEventTime) {
		this.todoEventTime = todoEventTime;
	}

	public ClassLevelService getClassLevelService() {
		return classLevelService;
	}

	public void setClassLevelService(ClassLevelService classLevelService) {
		this.classLevelService = classLevelService;
	}

	public ClassTypeService getClassTypeService() {
		return classTypeService;
	}

	public void setClassTypeService(ClassTypeService classTypeService) {
		this.classTypeService = classTypeService;
	}

	public CourseService getCourseService() {
		return courseService;
	}

	public void setCourseService(CourseService courseService) {
		this.courseService = courseService;
	}

	public List<ClassType> getOptionalClassTypeList() {
		return optionalClassTypeList;
	}

	public void setOptionalClassTypeList(List<ClassType> optionalClassTypeList) {
		this.optionalClassTypeList = optionalClassTypeList;
	}

	public List<ClassUnit> getSelectClassUnitList() {
		return selectClassUnitList;
	}

	public void setSelectClassUnitList(List<ClassUnit> selectClassUnitList) {
		this.selectClassUnitList = selectClassUnitList;
	}

	public Map<String, String> getCourseList() {
		return courseList;
	}

	public void setCourseList(Map<String, String> courseList) {
		this.courseList = courseList;
	}

	public Map<String, String> getClassLevelList() {
		return classLevelList;
	}

	public void setClassLevelList(Map<String, String> classLevelList) {
		this.classLevelList = classLevelList;
	}

	public String getSelClassLevelId() {
		return selClassLevelId;
	}

	public void setSelClassLevelId(String selClassLevelId) {
		this.selClassLevelId = selClassLevelId;
	}

	public String getSelCourseId() {
		return selCourseId;
	}

	public void setSelCourseId(String selCourseId) {
		this.selCourseId = selCourseId;
	}

	public String getSelLeadsId() {
		return selLeadsId;
	}

	public void setSelLeadsId(String selLeadsId) {
		this.selLeadsId = selLeadsId;
	}

	public List<LeadsClassEntity> getLeadsClassEntityList() {
		return leadsClassEntityList;
	}

	public void setLeadsClassEntityList(
			List<LeadsClassEntity> leadsClassEntityList) {
		this.leadsClassEntityList = leadsClassEntityList;
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	public Integer getYear() {
		return year;
	}

	public void setYear(Integer year) {
		this.year = year;
	}

	public Integer getWeek() {
		return week;
	}

	public void setWeek(Integer week) {
		this.week = week;
	}

	public List<LeadsDealHistory> getUpperLevelHistory() {
		return upperLevelHistory;
	}

	public void setUpperLevelHistory(List<LeadsDealHistory> upperLevelHistory) {
		this.upperLevelHistory = upperLevelHistory;
	}

	public List<LeadsDealHistory> getDeferHistory() {
		return deferHistory;
	}

	public void setDeferHistory(List<LeadsDealHistory> deferHistory) {
		this.deferHistory = deferHistory;
	}

	public List<LeadsDealHistory> getUpperClassHistory() {
		return upperClassHistory;
	}

	public void setUpperClassHistory(List<LeadsDealHistory> upperClassHistory) {
		this.upperClassHistory = upperClassHistory;
	}

	public LeadsDealHistoryService getLeadsDealHistoryService() {
		return leadsDealHistoryService;
	}

	public void setLeadsDealHistoryService(
			LeadsDealHistoryService leadsDealHistoryService) {
		this.leadsDealHistoryService = leadsDealHistoryService;
	}

	public Date getSelTrueValidEndTime() {
		return selTrueValidEndTime;
	}

	public void setSelTrueValidEndTime(Date selTrueValidEndTime) {
		this.selTrueValidEndTime = selTrueValidEndTime;
	}

	public Date getSelDeferStartTime() {
		return selDeferStartTime;
	}

	public void setSelDeferStartTime(Date selDeferStartTime) {
		this.selDeferStartTime = selDeferStartTime;
	}

	public Date getSelDeferEndTime() {
		return selDeferEndTime;
	}

	public void setSelDeferEndTime(Date selDeferEndTime) {
		this.selDeferEndTime = selDeferEndTime;
	}

	public Date getSelTrueValidStartTime() {
		return selTrueValidStartTime;
	}

	public void setSelTrueValidStartTime(Date selTrueValidStartTime) {
		this.selTrueValidStartTime = selTrueValidStartTime;
	}

	public Boolean getNewDefer() {
		return newDefer;
	}

	public void setNewDefer(Boolean newDefer) {
		this.newDefer = newDefer;
	}
}
